package com.auto.survey.activity.monitoring.point

import android.content.Intent
import android.graphics.Color
import android.text.TextUtils
import android.util.Log
import android.view.View
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.auto.survey.R
import com.auto.survey.activity.monitoring.IntentKey.KEY_INTENT_MEASURE_ID
import com.auto.survey.activity.monitoring.IntentKey.KEY_INTENT_OBJECT_ID
import com.auto.survey.activity.monitoring.IntentKey.KEY_INTENT_PROJET_ID
import com.auto.survey.activity.monitoring.IntentKey.KEY_INTENT_STATION_ID
import com.auto.survey.activity.monitoring.adapter.MontoringPointAdapter
import com.auto.survey.activity.monitoring.adapter.MontoringPointAdapter.OnPointClickItemListener
import com.auto.survey.activity.monitoring.adapter.MontoringPointAdapter.OnPointDeleteClickListener
import com.auto.survey.activity.monitoring.adapter.PointManagerAdapter
import com.auto.survey.activity.monitoring.dao.MonitoringRepository
import com.auto.survey.activity.monitoring.entity.MonitoringPoint
import com.auto.survey.activity.monitoring.entity.Point
import com.auto.survey.base.BaseActivity
import com.auto.survey.util.Helper
import com.auto.survey.widget.CentreToast
import com.auto.survey.widget.CommonTitle.Companion.TEXT_POSITON_RIGHT
import kotlinx.coroutines.launch

/**
 * 测点管理
 */
class ManagerPointActivity :BaseActivity(){

    private var projectId:Int = -1
    private var objectId:Int = -1

    private var stationId :Int = -1

    private var measureJson:String?= null

    private val database = MonitoringRepository()
    private var adapter: PointManagerAdapter? = null

    override fun initView() {
        projectId = intent.getIntExtra(KEY_INTENT_PROJET_ID, -1)
        objectId = intent.getIntExtra(KEY_INTENT_OBJECT_ID, -1)
        stationId = intent.getIntExtra(KEY_INTENT_STATION_ID, -1)
        measureJson = intent.getStringExtra(KEY_INTENT_MEASURE_ID) //用于测量后选点，传测量的值
        Log.i("ManagerPointActivity", "projectId = $projectId, objectId = $objectId, measureJson = $measureJson")


        mTopBar?.visibility = View.VISIBLE
        mTopBar?.setBackgroundResource(R.color.action_bar)

        mComment_title?.visibility = View.VISIBLE
        mComment_title?.setBackgroundResource(R.color.action_bar)
        mComment_title?.setTitle("测点管理")

        mComment_title?.setRightTitle("添加测点")
        mComment_title?.setTitleColor(Color.WHITE, TEXT_POSITON_RIGHT)
        mComment_title?.setOnRightTextClickListener(Helper.debounce{

            val intent = Intent()
            intent.putExtra(KEY_INTENT_OBJECT_ID, objectId)
            intent.setClass(this@ManagerPointActivity, CreatePointActivity::class.java)
            startActivity(intent)
        })

        val rv_points = findViewById<RecyclerView>(R.id.rv_manager_montoring_point)
        adapter = PointManagerAdapter()

        if (!TextUtils.isEmpty(measureJson)) {
            //选点后测量或测量后选点
            adapter?.setOnItemClickListener(object: PointManagerAdapter.OnPointClickItemListener {
                override fun ontItemPointClick(point: Point, position: Int) {
                    //{"KEY_MAP_POINT_SD":2.9816,"KEY_MAP_POINT_Y":860.7849,"KEY_MAP_POINT_X":1393.0453,"KEY_MAP_POINT_HZ":0.621620494047055,"KEY_MAP_POINT_HIGHT":0.0,"KEY_MAP_POINT_VA":1.620390744175635,"KEY_MAP_POINT_Z":12.8902}
                    measureJson?.let {
                        lifecycleScope.launch {
                            Log.i("ManagerPointActivity", "pointid = ${point.id}, objectId=${point.monitoringObjectId}")
                            val maxCount = database.getMonitoringPointSerilNum(objectId)?:0
                            database.createMonitoringPoint(Helper.jsonToPoint(it, point, objectId, stationId, maxCount))
                        }
                    }
                    finish()
                }
            })
        } else {
            adapter?.setOnDeleteClickListener(object : PointManagerAdapter.OnPointDeleteClickListener{
                override fun onDelteClick(point: Point, deletePosition: Int) {
                    CentreToast.instance.showText("删除")
                }

            })

            adapter?.setOnUpdateClickListener(object :PointManagerAdapter.OnPointUpdateClickListener{
                override fun onUpdateClick(point: Point, updatePosition: Int) {
                    CentreToast.instance.showText("编辑")
                }

            })
        }



        rv_points.layoutManager = LinearLayoutManager(this)
        rv_points.adapter = adapter

    }

    override fun onResume() {
        super.onResume()
        lifecycleScope.launch {
            database.getPointsByObjId(objectId).collect{
                adapter?.setData(it)
            }
        }


    }

    override fun getLayoutId(): Int {

        return R.layout.activity_manager_montoring_point
    }
}